package com.shwread.android.ui.widget;

import android.view.View;
import android.widget.RelativeLayout;

import com.shwread.android.qysw.activity.ReadingBookActivity;
import com.shwread.android.qysw10000136.R;

import org.geometerplus.android.fbreader.ButtonsPopupPanel;
import org.geometerplus.android.fbreader.PopupWindow;
import org.geometerplus.fbreader.fbreader.ActionCode;
import org.geometerplus.fbreader.fbreader.FBReaderApp;

/**
 * 
 * @Title BookmarkPopup.java
 * @Description 再次点击笔记时的popup
 * @author fw
 * @Crtdate 2013-12-6下午4:50:32
 *
 */
public class BookmarkPopup extends ButtonsPopupPanel {
	public final static String ID = "BookmarkPopup";

	public BookmarkPopup(FBReaderApp fbReader) {
		super(fbReader);
	}

	@Override
	public String getId() {
		return ID;
	}

	@Override
	public void createControlPanel(ReadingBookActivity activity, RelativeLayout root) {
		if (myWindow != null && activity == myWindow.getActivity()) {
			return;
		}

		myWindow = new PopupWindow(activity, root, PopupWindow.Location.Floating);
		
		addButton(ActionCode.BOOKMARK_CLIPBOARD, "复制", true, R.drawable.pop_button_background);
		addButton(ActionCode.BOOKMARK_BOOKREMARK, "笔记", true, R.drawable.pop_button_background);
		addButton(ActionCode.BOOKMARK_CLEAR, "删除", true, R.drawable.pop_button_background);
	}
	
	

	public void move(int selectionStartY, int selectionEndY) {
		if (myWindow == null) {
			return;
		}

		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
			RelativeLayout.LayoutParams.WRAP_CONTENT,
			RelativeLayout.LayoutParams.WRAP_CONTENT
		);
		layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
		final int verticalPosition;
		final int screenHeight = ((View)myWindow.getParent()).getHeight();
		final int diffTop = screenHeight - selectionEndY;//下方光标位置
		final int diffBottom = selectionStartY; //起始光标位置
		if (diffTop > diffBottom) {
			verticalPosition = diffTop > myWindow.getHeight() + 20
				? RelativeLayout.ALIGN_PARENT_BOTTOM : RelativeLayout.CENTER_VERTICAL;
		} else {
			verticalPosition = diffBottom > myWindow.getHeight() + 20
				? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.CENTER_VERTICAL;
		}

		layoutParams.addRule(verticalPosition);
		myWindow.setLayoutParams(layoutParams);
	}

}
